/*
 * 创建时间：17-9-23
 * 作者：sce
 * 功能描述：新功能的model表
 * 修改时间：
 */

import Sequelize from 'sequelize'
import sequelize from './../../database'

const Ability = sequelize.define('sys_ability', {
    id:{
        type: Sequelize.UUIDV4,
        defaultValue: Sequelize.UUIDV4,
        unique: true,
        allowNull: false,
        primaryKey:true
    },
    ability_title:{
        type:Sequelize.STRING,
        allowNull: false
    },
    ability_subtitle:{
        type:Sequelize.STRING
    },
    ability_description: {
        type:Sequelize.STRING,
        allowNull: false
    },
    picture_url1:{
        type:Sequelize.STRING
    },
    picture_url2:{
        type:Sequelize.STRING
    },
    picture_url3:{
        type:Sequelize.STRING
    },
    app_version:{
        type:Sequelize.STRING,
        allowNull: false
    },
    is_publish:{
        type:Sequelize.INTEGER,
        allowNull: false,
        defaultValue:1
    },
    type:{
        type:Sequelize.INTEGER,
        allowNull:false
    }
},{
    freezeTableName:false,
    tableName: 'sys_ability',
    updatedAt:'updated_at',
    createdAt:'created_at',
});
class AbilityModel{
    findById(id){
        return Ability.findById(id);
    }
    findListPublish(){
        return Ability.findAll({
            where:{
                is_publist:1
            }
        })
    }
    findListAll(){
        return Ability.findAll();
    }
    findByPage(record){
        return Ability.findAndCount({
            limit:record.page_size,
            offset: record.page_size * (record.page_no -1)
        })
    }
    insert(record){
        return Ability.create(record);
    }
    update(record){
        return Ability.update(record,
            {
                'where':{
                    'id': record.id
                },
                fields: ['ability_title','ability_subtitle', 'ability_description', 'picture_url1','picture_url2','picture_url3', 'app_version', 'type', 'is_publish']
            }
        )
    }
    deleteById(id){
        return Ability.destroy({
            where: {
                id: id
            }
        });
    }
}
module.exports = new AbilityModel();